//
//  GQHContractLoadingInfoController.m
//  Xanthium
//
//  Created by GuanQinghao on 16/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHContractModel.h"

#pragma mark Object

#pragma mark View
#import "GQHContractLoadingInfoView.h"

#pragma mark Controller
#import "GQHContractLoadingInfoController.h"
#import "GQHContractExpenseInfoController.h"

#import "GQHModelController.h"
#import "GQHLengthController.h"
#import "GQHBusinessController.h"
#import "GQHTransportController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHContractLoadingInfoController () <GQHContractLoadingInfoViewDelegate> {
    
#pragma mark Data
    /// 参数
    NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHContractLoadingInfoView *rootView;
    /// 弹出框控制器
    UIAlertController *alertController;
}

@end


@implementation GQHContractLoadingInfoController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHContractLoadingInfoView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_qh_qutationModel.qh_freightID forKey:@"sourcesInfoId"];
    [parameters setObject:_qh_qutationModel.qh_quoterID forKey:@"driverInfo"];
    [parameters setObject:_qh_qutationModel.qh_qutationID forKey:@"quotationId"];
    
    
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_loader atIndex:0];
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_loaderMobile atIndex:1];
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_departure atIndex:2];
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_loadingTime atIndex:3];
    [rootView.qh_contentArray qh_safetySetObject:@"" atIndex:4];
    [rootView.qh_contentArray qh_safetySetObject:@"" atIndex:5];
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_destination atIndex:6];
    [rootView.qh_contentArray qh_safetySetObject:@"请选择" atIndex:7];
    [rootView.qh_contentArray qh_safetySetObject:@"请选择" atIndex:8];
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_lengthWanted atIndex:9];
    [rootView.qh_contentArray qh_safetySetObject:_qh_qutationModel.qh_modelWanted atIndex:10];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"协议货物信息";
    [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
//    rowTitleArray = @[@"装货联系人",@"装货电话",@"装货地址",@"装货时间",@"收货联系人",@"收货电话",@"收货地址",@"业务类型",@"运输方式",@"车长",@"车辆类型"];
    
    switch (indexPath.section) {
            
//        case 0: {
//
//            switch (indexPath.row) {
//
//                case 0: {
//
//                    /// 装货联系人
//                    [self qh_alertWithTitle:@"请输入装货联系人" message:nil inputHandler:^(UITextField * _Nullable textField) {
//
//                        textField.placeholder = @"装货联系人";
//
//                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
//
//                        UITextField *inputTextField = alertController.textFields.firstObject;
//                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:0];
//                        [rootView.qh_tableView reloadData];
//
//                    } completion:nil];
//                }
//                    break;
//                case 1: {
//
//                    /// 装货联系电话
//                    [self qh_alertWithTitle:@"请输入装货电话" message:nil inputHandler:^(UITextField * _Nullable textField) {
//
//                        textField.placeholder = @"装货电话";
//                        textField.keyboardType = UIKeyboardTypeNumberPad;
//
//                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
//
//                        UITextField *inputTextField = alertController.textFields.firstObject;
//                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:1];
//                        [rootView.qh_tableView reloadData];
//
//                    } completion:nil];
//                }
//                    break;
//                case 2: {
//
//                    /// 装货地址
//                    [self qh_alertWithTitle:@"请输入装货地址" message:nil inputHandler:^(UITextField * _Nullable textField) {
//
//                        textField.placeholder = @"装货地址";
//
//                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
//
//                        UITextField *inputTextField = alertController.textFields.firstObject;
//                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:2];
//                        [rootView.qh_tableView reloadData];
//
//                    } completion:nil];
//                }
//                    break;
//                case 3: {
//
//                    /// 装货时间
//                    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
//                    datePicker.datePickerMode = UIDatePickerModeDateAndTime;
//                    [datePicker addTarget:self action:@selector(datePickerChangeValue:) forControlEvents:UIControlEventValueChanged];
//                    __weak typeof(datePicker) weakDatePicker = datePicker;
//
//                    alertController = [UIAlertController alertControllerWithTitle:@"请选择装货时间" message: nil preferredStyle:UIAlertControllerStyleAlert];
//                    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//
//                        textField.placeholder = @"请选择装货时间";
//                        textField.tag = 900;
//                        textField.inputView = weakDatePicker;
//                    }];
//
//                    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
//                    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//                        UITextField *inputTextField = alertController.textFields.firstObject;
//                        inputTextField = alertController.textFields.firstObject;
//                        if (inputTextField.text.length > 0) {
//
//                            [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:3];
//                            [rootView.qh_tableView reloadData];
//                        }
//                    }]];
//
//                    [self presentViewController:alertController animated:YES completion:nil];
//                }
//                    break;
//                default:
//                    break;
//            }
//        }
//            break;
        case 1: {
            
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 收货联系人
                    [self qh_alertWithTitle:@"请输入收货联系人" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"收货联系人";
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        [parameters setObject:inputTextField.text forKey:@"consignee"];
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:4];
                        [rootView.qh_tableView reloadData];
                        
                    } completion:nil];
                }
                    break;
                case 1: {
                    
                    /// 收货联系电话
                    [self qh_alertWithTitle:@"请输入收货电话" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"收货电话";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        [parameters setObject:inputTextField.text forKey:@"consigneePhone"];
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:5];
                        [rootView.qh_tableView reloadData];
                        
                    } completion:nil];
                }
                    break;
//                case 2: {
//
//                    /// 收货地址
//                    [self qh_alertWithTitle:@"请输入收货地址" message:nil inputHandler:^(UITextField * _Nullable textField) {
//
//                        textField.placeholder = @"收货地址";
//
//                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
//
//                        UITextField *inputTextField = alertController.textFields.firstObject;
////                        [parameters setObject:inputTextField.text forKey:@"businesstypecode"];
//                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:6];
//                        [rootView.qh_tableView reloadData];
//
//                    } completion:nil];
//                }
//                    break;
                default:
                    break;
            }
        }
            break;
        case 2: {
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 业务类型
                    GQHBusinessController *businessController = [[GQHBusinessController alloc] init];
                    businessController.qh_businessBlock = ^(NSString *businessID, NSString *businessName) {
                        
                        [parameters setObject:businessID forKey:@"businesstypecode"];
                        [rootView.qh_contentArray qh_safetySetObject:businessName atIndex:7];
                        [rootView.qh_tableView reloadData];
                    };
                    businessController.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:businessController animated:YES];
                }
                    break;
                case 1: {
                    
                    /// 运输方式
                    GQHTransportController *transportController = [[GQHTransportController alloc] init];
                    transportController.qh_transportBlock = ^(NSString *transportID, NSString *transportName) {
                        
                        [parameters setObject:transportID forKey:@"transporttype"];
                        [rootView.qh_contentArray qh_safetySetObject:transportName atIndex:8];
                        [rootView.qh_tableView reloadData];
                    };
                    transportController.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:transportController animated:YES];
                }
                    break;
                case 2: {
                    
                    /// 车长
                    GQHLengthController *lengthController = [[GQHLengthController alloc] init];
                    lengthController.qh_lengthBlock = ^(NSString *lengthID, NSString *lengthName) {
                        
                        [rootView.qh_contentArray qh_safetySetObject:lengthName atIndex:9];
                        [rootView.qh_tableView reloadData];
                    };
                    lengthController.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:lengthController animated:YES];
                }
                    break;
                case 3: {
                    
                    /// 车型
                    GQHModelController *modelController = [[GQHModelController alloc] init];
                    modelController.qh_modelBlock = ^(NSString *modelID, NSString *modelName) {
                        
                        [rootView.qh_contentArray qh_safetySetObject:modelName atIndex:10];
                        [rootView.qh_tableView reloadData];
                    };
                    modelController.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:modelController animated:YES];
                }
                    break;
                
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    NSString *consignee = rootView.qh_contentArray[4];
    //添加收货联系人,因为这个是单独添加的
    [parameters setObject:consignee forKey:@"consignee"];
    
    if ([consignee containsString:@""]) {
        
        [self showToastWithText:@"请输入收货联系人"];
        return;
    }
    
    if (!(consignee && consignee.length > 0)) {
        
        [self showToastWithText:@"请输入收货联系人"];
        return;
    }
    
    NSString *consigneePhone = rootView.qh_contentArray[5];
    [parameters setObject:consigneePhone forKey:@"consigneePhone"];
    
    NSString *businesstypecode =  rootView.qh_contentArray[7];
    if ([businesstypecode containsString:@"请选择"]) {
        
        [self showToastWithText:@"请选择业务类型"];
        return;
    }
    
    if (!(businesstypecode && businesstypecode.length > 0)) {
        
        [self showToastWithText:@"请选择业务类型"];
        return;
    }
    
    NSString *transporttype = rootView.qh_contentArray[8];
    if ([transporttype containsString:@"请选择"]) {
        
        [self showToastWithText:@"请选择运输方式"];
        return;
    }
    
    if (!(transporttype && transporttype.length > 0)) {
        
        [self showToastWithText:@"请选择请选择运输方式"];
        return;
    }
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybillAgreement/1/next"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [GQHNetHelper qh_requestGET:APIURLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (0 == [code integerValue]) {
            
            GQHContractExpenseInfoController *contractExpenseInfoController = [[GQHContractExpenseInfoController alloc] init];
            contractExpenseInfoController.qh_qutationModel = _qh_qutationModel;
            contractExpenseInfoController. qh_contractID = [data objectForKey:@"id"];
            contractExpenseInfoController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:contractExpenseInfoController animated:YES];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod
- (IBAction)datePickerChangeValue:(UIDatePicker *)sender {
    
    NSDate *date = sender.date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *dateString = [formatter stringFromDate:date];
    
    UITextField *inputTextField = (UITextField *)[alertController.view viewWithTag:900];
    inputTextField.text = dateString;
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
